package com.farm.bpm.form;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.activiti.engine.form.FormProperty;
import org.activiti.engine.form.TaskFormData;
import org.apache.struts2.json.JSONUtil;


public class FarmActivitiFormParser {
	private List<FormParseInter> parsers;
	private int inputWidth;

	/**
	 * 获得插件说明
	 * 
	 * @return
	 */
	public List<Map<String, String>> getPluginInfo() {
		List<Map<String, String>> list = new ArrayList<Map<String, String>>();
		for (FormParseInter parser : parsers) {
			Map<String, String> map = new HashMap<String, String>();
			map.put("type", parser.getfieldTypeFormat());
			map.put("describe", parser.getfieldTypeDescribes());
			list.add(map);
		}
		return list;
	}

	/**
	 * 解析表单
	 * 
	 * @param taskform
	 * @param variables
	 * @return
	 */
	public Form getForm(TaskFormData taskform, Map<String, Object> variables) {
		return iteratorForm(taskform, variables, new TaskFormDataHandleInter() {
			@Override
			public Field handle(String fieldType, List<String> parameter,
					String name, Field field) {
				for (FormParseInter parser : parsers) {
					parser.parse(fieldType, parameter, name, field);
				}
				return field;
			}
		});
	}

	/**
	 * 处理表单保存
	 * 
	 * @param taskform
	 */
	public void saveHandle(TaskFormData taskform) {
		iteratorForm(taskform, null, new TaskFormDataHandleInter() {
			@Override
			public Field handle(String fieldType, List<String> parameter,
					String name, Field field) {
				for (FormParseInter parser : parsers) {
					parser.saveHandle(fieldType, parameter, name, field);
				}
				return field;
			}
		});
	}

	public List<FormParseInter> getParsers() {
		return parsers;
	}

	public void setParsers(List<FormParseInter> parsers) {
		this.parsers = parsers;
	}

	public int getInputWidth() {
		return inputWidth;
	}

	public void setInputWidth(int inputWidth) {
		this.inputWidth = inputWidth;
	}

	/**
	 * 迭代form的字段定义
	 * 
	 * @param taskform
	 *            activiti表单
	 * @param variables
	 *            activiti流程变量集合
	 * @param handle
	 *            处理接口
	 * @return
	 */
	@SuppressWarnings("unchecked")
	private Form iteratorForm(TaskFormData taskform,
			Map<String, Object> variables, TaskFormDataHandleInter handle) {
		Form form = new Form();
		for (FormProperty property : taskform.getFormProperties()) {
			// 迭代activiti表单字段
			Field field = new Field();
			String fieldType = null;
			String name;
			List<String> parameter = new ArrayList<String>();
			try {
				// 解析用户自定义类型
				if (property.getName().indexOf("#{") > 0) {
					Map<String, Object> map = (Map<String, Object>) JSONUtil
							.deserialize("{"
									+ property.getName().split("\\#\\{")[1]);
					fieldType = map.get("type").toString();
					// 转义参数,如果再流程变量中存在该参数就，将该参数替换为变量值
					if(map.get("parameter")!=null){
						for (String para : (List<String>) map.get("parameter")) {
							if (variables.containsKey(para)) {
								parameter.add(variables.get(para).toString());
							} else {
								parameter.add(para);
							}
						}
					}
					name = property.getName().split("\\#\\{")[0];
				} else {
					name = property.getName();
					fieldType = property.getType().getName();
				}
			} catch (Exception e) {
				name = property.getName();
			}
			field.formWidth = inputWidth;
			field.id = property.getId();
			field.name = "fields." + property.getId();
			field.title = name;
			field.read = property.isReadable();
			field.write = property.isWritable();
			field.required = property.isRequired();
			field.value = property.getValue();
			if (fieldType.equals("enum")) {
				Map<String, String> values = (Map<String, String>) property
						.getType().getInformation("values");
				if (values != null) {
					field.setEnums(values);
				}
			}
			field = handle.handle(fieldType, parameter, name, field);
			form.getFields().add(field);
		}
		return form;
	}
}
